﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MemoryFormDeal
{
    public partial class AddForm : Form
    {
        ProgramSave save = new ProgramSave();
        int leftmemory;
        public AddForm(int leftm)
        {
            leftmemory = leftm;
            StartPosition = FormStartPosition.CenterParent;
            InitializeComponent();
        }

        private void AddOk_Click(object sender, EventArgs e)
        {
            if(IdInput.Text==""||NameInput.Text==""||PUseInput.Text=="")
            {
                MessageBox.Show("相关内容不能为空！","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            else if(!Regex.IsMatch(PUseInput.Text, @"^[+-]?\d*$") || !Regex.IsMatch(IdInput.Text, @"^[+-]?\d*$"))
            {
                MessageBox.Show("相关内容非法！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                PUseInput.ResetText();
                IdInput.ResetText();
                return;
            }
            else if(Convert.ToInt32(PUseInput.Text)> leftmemory)
            {
                
                MessageBox.Show("申请内存过大！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                PUseInput.ResetText();
            }
            else
            {
                save.No = Convert.ToInt32(IdInput.Text);
                save.PName = NameInput.Text;
                save.PMUse = Convert.ToInt32(PUseInput.Text);
                switch (ColourChoose.SelectedIndex)
                {
                    case 0:
                        save.colour = "Red";
                        break;
                    case 1:
                        save.colour = "Yellow";
                        break;
                    case 2:
                        save.colour = "Blue";
                        break;
                    case 3:
                        save.colour = "Green";
                        break;
                    default:
                        save.colour = "Purple";
                        break;
                }
                switch (TypeChoose.SelectedIndex)
                {
                    case 0:
                        save.pType = 0;
                        break;
                    default:
                        save.pType = 1;
                        break;
                }
                InitForm lform1 = (InitForm)this.Owner;
                lform1.GetReturnInfo(save);    
                this.Close();
                lform1.AddP();
           }

        }
    }
}
